import Vue from "vue";
import VueRouter from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

import { routers } from "./routerMaps";
import { base } from "./base";
import { getToken, removeToken } from "@/utils/token";
const routerReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function push(location) {
  return routerReplace.call(this, location).catch((error) => error);
};
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch((error) => error);
};

Vue.use(VueRouter);

const createRouter = () =>
  new VueRouter({
    mode: "history",
    routes: [...base, ...routers],
  });

const router = createRouter();

const whiteList = ["/login", "/404","/register","/reset",'/teacher','/query','/labAdministration','/useResults']; 

NProgress.configure({
  showSpinner: false,
});

router.beforeEach(async (to, from, next) => {
  NProgress.start();
  
  const hasToken = localStorage.getItem('data');


  if (hasToken == 'res.data') {

    next()
  } else {
    console.log('ffff', )
    if (whiteList.indexOf(to.path) !== -1) {

      console.log('dddd', )
      next();
    } else {

      next(`/login?redirect=${to.path}`);
    }
  }
});

router.afterEach(() => {
  NProgress.done();
});

function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // the relevant part
}

export default router;
